package com.training.controller;

import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.Ret;
import com.training.common.model.User;
import com.training.interceptor.LoginInterceptor;
import com.training.service.UserService;
import com.training.service.UserServiceImp;

@Clear({LoginInterceptor.class, SessionInViewInterceptor.class})
public class LoginController extends Controller {

    @Inject(UserServiceImp.class)
    private UserService userService;

    public void randomcode(){
        renderCaptcha();
    }

    public void index(){
        String username = getPara("username");
        String password = getPara("password");
        User user = userService.login(username,password);
        if(user==null){
            renderJson(Ret.fail("msg","账号或密码错误，登录失败！"));
        }else{
            setSessionAttr("trainingsession",user);
            renderJson(Ret.ok("msg","登录成功！"));
        }
    }

    public void logout(){
        removeSessionAttr("trainingsession");
        redirect("/");
    }


}
